﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Lib.Common;
using SDT.Model;
using SDT.Site.Areas.User.ViewModels;
using SDT.Site.Properties;

namespace SDT.Site.Areas.User.Controllers
{
    [Authorize]
    public class WishlistController : Controller
    {
        //
        // GET: /User/Wishlist/
        SDTDataContext eDataContext = new SDTDataContext();
        public ActionResult Index(int? page)
        {
            try
            {                
                var c = CustomerManager.GetUser(User.Identity.Name);
                if (c != null)
                {
                    if (page == null)
                        page = 1;
                    string order = UserState.getUserStateFromRequest("product.order", "order", "ID").ToString();
                    string orderDir = UserState.getUserStateFromRequest("product.orderDir", "orderDir", "desc").ToString();
                    string keyword = UserState.getUserStateFromRequest("product.Keyword", "Keyword", "").ToString();
                    int pageSize = int.Parse(UserState.getUserStateFromRequest("product.pageSize", "PageSize", 20).ToString());
                    var pros = ProductManager.GetWishlist(
                        keyword,c.ID, order, orderDir, page.Value - 1, pageSize);
                    ViewData["orderDir"] = orderDir;
                    ViewData["order"] = order;
                    var listProductTypes = eDataContext.ProductTypes.ToList();
                    return View(new WishlistViewModels
                    {
                        Products = pros,
                        Keyword = keyword
                    });
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        public ActionResult Create(int id)
        {
            var c = CustomerManager.GetUser(User.Identity.Name);
            if (c != null)
            {
                var w = new Wishlist();
                w.CustomerID = c.ID;
                w.ProductID = id;
                var has = eDataContext.Wishlists.Where(e => e.ProductID == id && e.CustomerID == c.ID).SingleOrDefault();
                if (has == null)
                {
                    eDataContext.Wishlists.InsertOnSubmit(w);
                    eDataContext.SubmitChanges();
                }
            }
            return RedirectToAction("index");
        }
        public ActionResult Delete(int id)
        {
            try
            {
                var c = CustomerManager.GetUser(User.Identity.Name);
                if (c != null)
                {
                    var item = eDataContext.Wishlists.SingleOrDefault(e => e.ProductID == id && e.CustomerID == c.ID);
                    return View(item);
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // POST: /Order/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var c = CustomerManager.GetUser(User.Identity.Name);
                if (c != null)
                {

                    var item = eDataContext.Wishlists.SingleOrDefault(e => e.ProductID == id && e.CustomerID == c.ID);

                    if (item != null)
                    {
                        eDataContext.Wishlists.DeleteOnSubmit(item);
                        eDataContext.SubmitChanges();
                    }
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

    }
}
